home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / system / epuptm17.zip / UPTIME.C < prev    next >
C/C++ Source or Header  |  1994-05-15  |  10KB  |  339 lines

  1. #ifndef __TINY__
  2. #error Use TINY memory model--Don't forget to turn off debug info
  3. #endif
  4.  
  5. #include <stdio.h>
  6. #include <dos.h>
  7. #include <dir.h>
  8. #include <time.h>
  9. #include <string.h>
  10. #include <bios.h>
  11. #include <sys\stat.h>
  12. #include <stdlib.h>
  13. #include <ctype.h>
  14. #include <conio.h>
  15.  
  16.  
  17. #define VER "v1.7"
  18. #define RIGHT       0x01
  19. #define LEFT       0x02
  20. #define TRUE 1
  21. #define FALSE 0
  22. #define NUMTAIL 20
  23.  
  24. void
  25. touch(char[]);
  26. void
  27. main(void);
  28. char           *
  29. upit(char[]);
  30. void
  31. writeout(void);
  32. void
  33. help(void);
  34. void
  35. printlog(void);
  36. void
  37. add2log(void);
  38. void
  39. getlog(char *);
  40. void
  41. tail(void);
  42.  
  43. char            filename[255], bday[4], bmon[4], ampm[3];
  44. char            set = FALSE, log = FALSE, needhelp = FALSE, cnt, found,showtail = FALSE;
  45. struct ffblk    ffblk;
  46. struct time     curtime;
  47. struct date     curdate;
  48. struct tm       old, *new;
  49. time_t          thetime;
  50. int             sec, min, day, hour, bdate, bhour, bmin, bsec, byear;
  51. struct COUNTRY cinfo;
  52.  
  53. void
  54. main(void)
  55. {  strcpy(ampm,"am");
  56.    strcpy(filename, searchpath(_argv[0]));
  57.    country(0,&cinfo);
  58.    for (cnt = 1; cnt + 1 <= _argc; cnt++) {
  59.       found = FALSE;
  60.       if (strcmp("SET", upit(_argv[cnt])) == 0)
  61.          set = found = TRUE;
  62.       if (strcmp("LOG", upit(_argv[cnt])) == 0)
  63.          log = found = TRUE;
  64.       if (strcmp("TAIL",upit(_argv[cnt])) == 0)
  65.          showtail = found = TRUE;
  66.       if (!found) {
  67.          needhelp = TRUE;
  68.          break;
  69.       }
  70.    }
  71.    if (needhelp) {
  72.       help();
  73.       exit(1);
  74.    }
  75.    if (set)
  76.       touch(filename);
  77.    if (log){
  78.       if (set)
  79.            add2log();
  80.       if (!set&&!showtail)
  81.          printlog();
  82.       if (!set&&showtail){
  83.         printf("Error:  Cannot combine \"log\" and \"tail\" without \"set\"\n");
  84.         exit(1);
  85.       }
  86.    }
  87.    if (set) //&&!showtail)
  88.       puts("...");
  89.    if (showtail){
  90.      if (set)
  91.        printf("\n");
  92.      tail();
  93.    }
  94.    if (_argc==1)
  95.       writeout();
  96.    fcloseall();
  97. }           /* main */
  98.  
  99. void
  100. touch(char name[])
  101. {
  102.    FILE           *file;
  103.    char            tmp;
  104.  
  105.    int             modifiers;
  106.  
  107.    modifiers = bioskey(2);
  108.    if (!(modifiers & RIGHT || modifiers & LEFT)) {
  109.       printf("Updating launch time");
  110.       file = fopen(name, "r+");
  111.       fseek(file, SEEK_SET, 0);
  112.       fread(&tmp, 1, 1, file);
  113.       rewind(file);
  114.       fwrite(&tmp, 1, 1, file);
  115.  
  116.    } else {
  117.       printf("Aborting update of %s\n", _argv[0]);
  118.       exit(1);
  119.    }
  120. }           /* touch */
  121.  
  122. char           *
  123. upit(char str[])
  124. {
  125.    int             i;
  126.  
  127.    for (i = 0; i < strlen(str); i++) {
  128.       str[i] = toupper(str[i]);
  129.    }
  130.    return str;
  131. }
  132.  
  133.  
  134. void
  135. writeout(void)
  136. {
  137.    char            tz[257], *oldtz;
  138.  
  139.    tz[0] = '\0';
  140.    oldtz = getenv("TZ");
  141.    if (oldtz != NULL) {
  142.       strcat(tz, "TZ=");
  143.       putenv(tz);
  144.    }
  145.    findfirst(filename, &ffblk, FA_DIREC);
  146.    old.tm_sec = 2 * (ffblk.ff_ftime & 31);
  147.    old.tm_min = (ffblk.ff_ftime & 2016) >> 5;
  148.    old.tm_hour = (ffblk.ff_ftime & 63488) >> 11;
  149.    old.tm_mday = (ffblk.ff_fdate & 31);
  150.    old.tm_mon = ((ffblk.ff_fdate & 480) >> 5) - 1;
  151.    old.tm_year = ((ffblk.ff_fdate & 65024) >> 9) + 80;
  152.    mktime(&old);
  153.  
  154.    thetime = time(NULL);
  155.    new = localtime(&thetime);
  156.  
  157.    sec = (*new).tm_sec - old.tm_sec;
  158.    min = (*new).tm_min - old.tm_min;
  159.    if (sec < 0) {
  160.       sec += 60;
  161.       min--;
  162.    }
  163.    hour = (*new).tm_hour - old.tm_hour;
  164.    if (min < 0) {
  165.       min += 60;
  166.       hour--;
  167.    }
  168.    day = (*new).tm_yday - old.tm_yday + (365 * ((*new).tm_year - old.tm_year));
  169.    if (hour < 0) {
  170.       hour += 24;
  171.       day--;
  172.    }
  173.    sscanf(asctime(&old), "%3s %3s %d %d:%d:%d %d", bday, bmon, &bdate, &bhour, &bmin, &bsec, &byear);
  174.    if (bhour >= 12&&cinfo.co_time==0) {
  175.       bhour -= 12;
  176.       ampm[0] = 'p';
  177.    }// else
  178.     //  ampm[0] = '\0';
  179.  
  180.    if (bhour == 0&&cinfo.co_time==0)
  181.       bhour = 12;
  182.    if (day < 0)
  183.       printf("Error:  System clock has been changed since bootup\n\n");
  184.    else if (day != 0)
  185.       switch (cinfo.co_date){
  186.        case 0:printf("System launched at %2.2d%c%2.2d%c%2.2d%s on %s, %s %d, %d\nUp for %d day(s), %2.2d%c%2.2d%c%2.2d\n", bhour,cinfo.co_tmsep[0], bmin,cinfo.co_tmsep[0], bsec, ampm, bday, bmon, bdate, byear, day, hour,cinfo.co_tmsep[0], min,cinfo.co_tmsep[0], sec);
  187.               break;
  188.        case 1:printf("System launched at %2.2d%c%2.2d%c%2.2d on %s, %d %s, %d\nUp for %d day(s), %2.2d%c%2.2d%c%2.2d\n", bhour,cinfo.co_tmsep[0], bmin,cinfo.co_tmsep[0], bsec, bday, bdate, bmon, byear, day, hour,cinfo.co_tmsep[0], min,cinfo.co_tmsep[0], sec);
  189.               break;
  190.        case 2:printf("System launched at %2.2d%c%2.2d%c%2.2d on %s, %d %s, %d\nUp for %d day(s), %2.2d%c%2.2d%c%2.2d\n", bhour,cinfo.co_tmsep[0], bmin,cinfo.co_tmsep[0], bsec, bday, byear, bmon, bdate, day, hour,cinfo.co_tmsep[0], min,cinfo.co_tmsep[0], sec);
  191.               break;
  192.        }
  193.    else
  194.       switch (cinfo.co_date){
  195.        case 0:printf("System launched at %2.2d%c%2.2d%c%2.2d%s on %s, %s %d, %d\nUp for %2.2d%c%2.2d%c%2.2d\n", bhour,cinfo.co_tmsep[0], bmin,cinfo.co_tmsep[0], bsec, ampm, bday, bmon, bdate, byear, hour,cinfo.co_tmsep[0], min,cinfo.co_tmsep[0], sec);
  196.               break;
  197.        case 1:printf("System launched at %2.2d%c%2.2d%c%2.2d on %s, %d %s, %d\nUp for %2.2d%c%2.2d%c%2.2d\n", bhour,cinfo.co_tmsep[0], bmin,cinfo.co_tmsep[0], bsec, bday, bdate, bmon, byear, hour,cinfo.co_tmsep[0], min,cinfo.co_tmsep[0], sec);
  198.               break;
  199.        case 2:printf("System launched at %2.2d%c%2.2d%c%2.2d on %s, %d %s, %d\nUp for %2.2d%c%2.2d%c%2.2d\n", bhour,cinfo.co_tmsep[0], bmin,cinfo.co_tmsep[0], bsec, bday, byear, bmon, bdate, hour,cinfo.co_tmsep[0], min,cinfo.co_tmsep[0], sec);
  200.               break;
  201.        }
  202.    if (oldtz != NULL) {
  203.       strcpy(tz, oldtz);
  204.       putenv(tz);
  205.    }
  206. }
  207.  
  208. void
  209. help(void)
  210. {
  211.    printf("\nUptime %s                                                  by Eric S. Peters\n", VER);
  212.    printf("Compiled at %s, on %s\n\n", __TIME__, __DATE__);
  213.    puts("Uptime will tell you how long your computer has been running since the last");
  214.    puts("time it was launched.  Executed with no parameters, it returns the time elapsed");
  215.    puts("since bootup.\n");
  216.    puts("SET\t: Will set the launchtime.  This needs to be in the AUTOEXEC.BAT file.");
  217.    puts("\t: If either shift key is held, the \"set\" is aborted.");
  218.    puts("LOG\t: Will print the log, or add to the log if called with \"set\".");
  219.    puts("\t: The log file will be in the same directory as uptime.");
  220.    puts("TAIL\t: Will print the last 20 entries in the log.");
  221.    puts("\t: Can be combined with \"set\" with no problem.");
  222.    puts("\t: Can only be combined with \"log\" if \"set\" is called as well.");
  223.    puts("\nUptime can be copied and distributed freely.\n");
  224.    puts("I used to think that it would be pretty hard to find a bug in a small program");
  225.    puts("like this, but I was wrong.  I can be reached at peters@cs.colostate.edu.\n");
  226. }
  227.  
  228. void
  229. printlog()
  230. {
  231.    char            logname[255], line[81], ans, tail[NUMTAIL][81];
  232.    FILE           *log;
  233.    int             cnt = 0,num2prn=0;
  234.  
  235.    getlog(logname);
  236.    log = fopen(logname, "r");
  237.    if (log != NULL) {
  238.       while (fgets(line, 81, log) != NULL) {
  239.          printf("%s", line);
  240.          if (++cnt % 24 == 0 && eof(log) != 0) {
  241.             printf("(T)ail, (Q)uit, or anything else\r");
  242.             ans = toupper(getch());
  243.             while(kbhit())
  244.                getch();
  245.             switch (ans) {
  246.             case 'Q':
  247.             case 27:
  248.                exit(0);
  249.                break;
  250.             case 'T':
  251.                      cnt = 0;
  252.                      fseek(log, 0, SEEK_SET);
  253.                      while (fgets(tail[cnt++ % NUMTAIL], 81, log) != NULL);
  254.                      num2prn=(--cnt);
  255.                      if (num2prn>20)
  256.                        clrscr();
  257.                      if (num2prn>NUMTAIL)
  258.                        num2prn=NUMTAIL;
  259.                      printf("Last %d Launch(es):\n\n", num2prn);
  260.                      for (ans = 1; ans <= num2prn;